#include "threadPool.h"
int initThreadPool(threadPool_t* pthreadPool, int workerNum){
    pthreadPool->workerNum = workerNum;
    pthreadPool->tidArr = (pthread_t*)calloc(pthreadPool->workerNum, sizeof(pthread_t));
    initQueue(&pthreadPool->taskQueue);
    return 0;
}
int main(int argc, char* argv[]){
    // ./server 192.168.19.147 1234 1
    FILE* fp = fopen("server.conf", "r");
    char ip[256];
    char port[256];
    char threadNum[256];

    fscanf(fp, "%s %s %s", ip, port, threadNum);
    fclose(fp);

    NETDISK_LOG_INFO(LOG_INFO, ip);

    //ARGS_CHECK(argc, 4);
    threadPool_t threadPool;
    int workerNum = atoi(threadNum);
    initThreadPool(&threadPool, workerNum);
    // 初始化tcp:
    int sockfd;
    tcpInit(ip, port, &sockfd);
    // 创建子线程
    makeWorker(&threadPool);
    // 监听sockfd
    int epfd = epoll_create(1);
    epollAdd(epfd, sockfd);
    struct epoll_event readyset[1024];
    while(1){
        int readynum =  epoll_wait(epfd, readyset, 1024, -1);
        for(int i = 0; i < readynum; i++){
            if(readyset[i].data.fd == sockfd){
                int netfd = accept(sockfd, NULL, NULL);
                printf("1 client connected!\n");
                pthread_mutex_lock(&threadPool.taskQueue.mutex);
                enQueue(&threadPool.taskQueue, netfd);
                printf("1 netfd enqueue!\n");
                pthread_cond_signal(&threadPool.taskQueue.cond);
                pthread_mutex_unlock(&threadPool.taskQueue.mutex);
            }
        }
    }
}
